<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
  <title>args (Array)</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
</head>
<body class="standalone-code">
  <pre><span class="ruby-comment cmt"># File enhanced_arg_parser.rb, line 121</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">args</span> <span class="ruby-operator">*</span><span class="ruby-identifier">settings_array</span>
    <span class="ruby-comment cmt"># self will be [1, 2, {:optional_2 =&gt; 7}]</span>
    <span class="ruby-comment cmt"># or           [1, {:req_2 =&gt; 2, :optional_2 =&gt; 7}]</span>
    <span class="ruby-comment cmt"># or           [1, 2, 3, 4]</span>
    <span class="ruby-comment cmt"># or           [1, 2]</span>
    
    <span class="ruby-identifier">args_passed</span> = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">dup</span>
    
    <span class="ruby-identifier">all_assigned</span> = []
    <span class="ruby-identifier">already_named_one</span> = <span class="ruby-keyword kw">false</span>
    <span class="ruby-comment cmt"># note that having a single array means just optionals</span>
    <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">settings_array</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">class</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Array</span>
      <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">settings_array</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">1</span> <span class="ruby-comment cmt"># they have more than just one array of optionals</span>
        <span class="ruby-identifier">requireds</span> = <span class="ruby-identifier">settings_array</span>[<span class="ruby-value">0</span>]
        <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">settings_array</span>[<span class="ruby-value">1</span>].<span class="ruby-identifier">class</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Array</span> <span class="ruby-comment cmt"># case of [:a, :b], {:c =&gt; 3}, {:d =&gt; 4}</span>
          <span class="ruby-identifier">optionals</span> = <span class="ruby-identifier">settings_array</span>[<span class="ruby-value">1</span>]
          <span class="ruby-identifier">raise</span> <span class="ruby-constant">MalformedArguments</span>.<span class="ruby-identifier">new</span>(<span class="ruby-node">&quot;you can have at most two arrays passed--the requireds then the optionals--I got #{settings_array}&quot;</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">settings_array</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">2</span>
        <span class="ruby-keyword kw">else</span>
          <span class="ruby-comment cmt"># case of [:a, :b], {:c =&gt; 3}, {:d =&gt; 4}</span>
          <span class="ruby-identifier">optionals</span> = <span class="ruby-identifier">settings_array</span>[<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">-1</span>]
        <span class="ruby-keyword kw">end</span>
      <span class="ruby-keyword kw">else</span>
        <span class="ruby-comment cmt"># case of [:a, :b]</span>
        <span class="ruby-identifier">requireds</span> = <span class="ruby-identifier">settings_array</span>[<span class="ruby-value">0</span>]
        <span class="ruby-identifier">optionals</span> = []
      <span class="ruby-keyword kw">end</span>
    <span class="ruby-keyword kw">else</span>
      <span class="ruby-comment cmt"># case of {:c =&gt; 3}, {:d =&gt; 4}</span>
      <span class="ruby-identifier">requireds</span> = []
      <span class="ruby-identifier">optionals</span> = <span class="ruby-identifier">settings_array</span>
    <span class="ruby-keyword kw">end</span>
    
    <span class="ruby-comment cmt"># get any nameds from the ending hash</span>
    <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">args_passed</span>[<span class="ruby-value">-1</span>].<span class="ruby-identifier">class</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Hash</span>
      <span class="ruby-identifier">ending_hash</span> = <span class="ruby-identifier">args_passed</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">dup</span>
    <span class="ruby-keyword kw">else</span>
      <span class="ruby-identifier">ending_hash</span> = {} <span class="ruby-comment cmt"># ltodo optimize this out?</span>
    <span class="ruby-keyword kw">end</span>

    <span class="ruby-identifier">requireds</span>.<span class="ruby-identifier">each</span>{ <span class="ruby-operator">|</span><span class="ruby-identifier">name</span><span class="ruby-operator">|</span>
      <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">ending_hash</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">name</span>)
        <span class="ruby-identifier">all_assigned</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">ending_hash</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">name</span>)
        <span class="ruby-identifier">already_named_one</span> = <span class="ruby-keyword kw">true</span> <span class="ruby-comment cmt">#they 'have' to name everything after their first named one, or I guess there'd be ambiguity as to which they meant the one after their first named one to actually be....well...at least we'll pretend they have to :)  I guess the only thing is it causes them to get a cross-eyed as to the one that came next like arg1, this_ones_in_the_hash_actually, arg3_looks_like_arg2_unless_you_go_cross_eyed, hash_one_is_here_now!</span>
      <span class="ruby-keyword kw">else</span>
        <span class="ruby-identifier">raise</span> <span class="ruby-constant">MalformedArguments</span>.<span class="ruby-identifier">new</span>(<span class="ruby-node">&quot;not enough required arguments  or missing when go to (#{name}) #{self.inspect}&quot;</span>) <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">args_passed</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span> <span class="ruby-comment cmt"># have to do this one by one in case we hit one, above</span>
        <span class="ruby-identifier">raise</span> <span class="ruby-constant">MalformedArguments</span>.<span class="ruby-identifier">new</span>(<span class="ruby-node">&quot;once you name one you must keep naming arguments (missing #{name}) #{self.inspect}&quot;</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">already_named_one</span>
        <span class="ruby-identifier">all_assigned</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">args_passed</span>.<span class="ruby-identifier">shift</span>
      <span class="ruby-keyword kw">end</span>
    }

    <span class="ruby-keyword kw">until</span> <span class="ruby-identifier">optionals</span>.<span class="ruby-identifier">empty?</span>
      <span class="ruby-identifier">name</span> = <span class="ruby-identifier">optionals</span>.<span class="ruby-identifier">shift</span>
      <span class="ruby-identifier">raise</span> <span class="ruby-constant">MalformedArguments</span>.<span class="ruby-identifier">new</span>(<span class="ruby-node">&quot;must be hash #{name.inspect}&quot;</span>) <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">name</span>.<span class="ruby-identifier">class</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Hash</span>
      <span class="ruby-identifier">raise</span> <span class="ruby-constant">MalformedArguments</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value str">'a setting array can only be one :variable =&gt; item!'</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">name</span>.<span class="ruby-identifier">to_s</span>) <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">name</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">==</span> <span class="ruby-value">1</span>
      <span class="ruby-identifier">name2</span> = <span class="ruby-identifier">name</span>.<span class="ruby-identifier">keys</span>[<span class="ruby-value">0</span>]
      <span class="ruby-identifier">default_value</span> = <span class="ruby-identifier">name</span>[<span class="ruby-identifier">name2</span>]
      <span class="ruby-identifier">name</span> = <span class="ruby-identifier">name2</span>
      
      <span class="ruby-identifier">raise</span> <span class="ruby-constant">MalformedArguments</span>.<span class="ruby-identifier">new</span>(<span class="ruby-node">&quot;variable name must be symbol #{name} -- also be mindful that if it's just optionals, they must NOT be in an array -- initial arrays are for requireds, only&quot;</span>) <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">name</span>.<span class="ruby-identifier">class</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Symbol</span> <span class="ruby-comment cmt"># ltodo should we not be symbol restrictive? :)</span>

      <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">ending_hash</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">name</span>) <span class="ruby-comment cmt"># allow for default to nil TOTEST</span>
        <span class="ruby-identifier">all_assigned</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">ending_hash</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">name</span>)
        <span class="ruby-identifier">raise</span> <span class="ruby-constant">MalformedArguments</span>.<span class="ruby-identifier">new</span>(<span class="ruby-node">&quot;seems you passed a named parameter of a required one but still have some extra unnamed ones extra in there (on #{name.inspect}) or you didnt pass some that were required}&quot;</span>) <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">args_passed</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
        <span class="ruby-identifier">already_named_one</span> = <span class="ruby-keyword kw">true</span>
      <span class="ruby-keyword kw">else</span> <span class="ruby-comment cmt"># if there are still 'unused' unnamed variables then use them, else use the default</span>
        <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">args_passed</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span>
          <span class="ruby-identifier">all_assigned</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">args_passed</span>.<span class="ruby-identifier">shift</span>
          <span class="ruby-identifier">raise</span> <span class="ruby-constant">MalformedArguments</span>.<span class="ruby-identifier">new</span>(<span class="ruby-node">&quot;once you name one you must keep naming to avoid ambiguity #{name}&quot;</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">already_named_one</span>
        <span class="ruby-keyword kw">else</span>
          <span class="ruby-identifier">all_assigned</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">default_value</span>
        <span class="ruby-keyword kw">end</span>
      <span class="ruby-keyword kw">end</span>

    <span class="ruby-keyword kw">end</span>    
    <span class="ruby-identifier">raise</span> <span class="ruby-constant">MalformedArguments</span>.<span class="ruby-identifier">new</span>(<span class="ruby-node">&quot;unknown vars! #{ending_hash.inspect}&quot;</span>) <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">ending_hash</span>.<span class="ruby-identifier">empty?</span>
    <span class="ruby-identifier">raise</span> <span class="ruby-constant">MalformedArguments</span>.<span class="ruby-identifier">new</span>(<span class="ruby-node">&quot;too many vars! #{args_passed.inspect}&quot;</span>) <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">args_passed</span>.<span class="ruby-identifier">empty?</span>
    
    <span class="ruby-identifier">all_assigned</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">==</span> <span class="ruby-value">1</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">all_assigned</span>[<span class="ruby-value">0</span>] <span class="ruby-operator">:</span> <span class="ruby-identifier">all_assigned</span> <span class="ruby-comment cmt"># accomodate for abc = args.get :abc =&gt; 3 # which passes out an array and assigns abc an array if there's only one -- anyway with only one the outgoing array isn't split right</span>
  <span class="ruby-keyword kw">end</span></pre>
</body>
</html>